package cn.xclink.kernel.web;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.xclink.common.web.BaseController;
import cn.xclink.common.web.ExecuteResult;
import cn.xclink.common.web.bind.annotation.CurrentUser;
import cn.xclink.kernel.entity.Resource;
import cn.xclink.kernel.entity.Role;
import cn.xclink.kernel.entity.User;
import cn.xclink.kernel.service.ResourceService;
import cn.xclink.kernel.service.RoleService;

@Controller
@RequestMapping("system/role")
public class RoleController extends BaseController<Role> {

	public static String LEFT_DIVID = "sys_role_left_div";
	public static String RIGHT_DIVID = "sys_role_right_div";
	
	@Autowired
	private RoleService roleService;
	
	@Autowired
	private ResourceService resourceService;

	@ModelAttribute
	public void init(Model model) {
		model.addAttribute("left_divid", LEFT_DIVID);
		model.addAttribute("right_divid", RIGHT_DIVID);
	}

	@RequiresPermissions("org:view")
	@RequestMapping
	public String index() {
		return "system/role/index";
	}
	
	@RequiresPermissions("role:view")
	@RequestMapping("/list")
	public String list(@CurrentUser User currentUser, Role role,Model model, HttpServletRequest request,
			@RequestParam(value="pageNum",required=false) Integer pageNum, 
    		@RequestParam(value="pageSize",required=false) Integer pageSize) {
		model.addAttribute("pageInfo", roleService.findPageInfo(role, pageNum, pageSize));
		model.addAttribute("role", role);
		return "system/role/list";
	}
	
	@RequiresPermissions("role:create")
	@RequestMapping(value = "/create", method = RequestMethod.GET)
	public String createForm(@CurrentUser User currentUser, Model model,HttpServletRequest request) {
		//当前用户有权限分配的资源
		//List<Resource> resourceList = resourceService.findResourceByUserId(currentUser.getUserId());
		List<Resource> resourceList = resourceService.findAll();
		model.addAttribute("resourceList", resourceList);
		return "system/role/create";
	}
	
	@RequiresPermissions("role:create")
	@RequestMapping(value = "/create", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> create(@CurrentUser User currentUser,Role role, Model model, HttpServletRequest request) {
		try {
			roleService.saveRole(role);
			return  ExecuteResult.jsonReturn(200);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage());
			return ExecuteResult.jsonReturn(300, e.getMessage(),false);
		}
	}
	
	@RequiresPermissions("role:edit")
	@RequestMapping(value = "/{id}/edit", method = RequestMethod.GET)
	public String editForm(@CurrentUser User currentUser,@PathVariable("id") Long id, Model model,HttpServletRequest request) {
		
		Role role =  roleService.get(id);
		model.addAttribute("role",role);
		//当前用户有权限分配的资源
		//List<Resource> resourceList = resourceService.findResourceByUserId(currentUser.getUserId());
		List<Resource> resourceList = resourceService.findAll();
		//已经分配的资源
		List<Resource> accessResourceList = resourceService.findResourceByRoleId(id);
		if(accessResourceList.size()>0){
			model.addAttribute("resourceIds",Arrays.toString(resourceService.findResourceIds(accessResourceList)).replace("[", "").replace("]", "").replace(" ", ""));
		}
		model.addAttribute("resourceList", resourceService.getResourceTree(resourceList,accessResourceList));
		return "system/role/edit";
		
	}

	@RequiresPermissions("role:edit")
	@RequestMapping(value = "/edit", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> edit(@CurrentUser User currentUser,Role role,Model model,HttpServletRequest request) {
		try {
			roleService.updateRole(role);
			return ExecuteResult.jsonReturn(200,RIGHT_DIVID);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage());
			return ExecuteResult.jsonReturn(300, e.getMessage(),false);
		}
	}
	
	@RequiresPermissions("role:delete")
	@RequestMapping(value = "/{id}/delete", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> delete(@CurrentUser User currentUser, @PathVariable("id") Long id,HttpServletRequest request) {
		try {
			roleService.delete(id);
			return ExecuteResult.jsonReturn(200,RIGHT_DIVID);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage());
			return ExecuteResult.jsonReturn(300, e.getMessage(),false);
		}
	}
}